import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-auth',
  templateUrl: './auth.component.html',
  styleUrls: ['./auth.component.scss']
})
export class AuthComponent implements OnInit {

  pageType = '';
  lang = '';

  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private translate: TranslateService,
  ) { }

  ngOnInit(): void {
    const urls = this.router.url.split('/');
    this.pageType = urls[urls.length - 1];

    // 初始化的时候进行默认语言设置
    const value = localStorage.getItem('admin-lang');
    this.lang = value || 'zh-CN';
    this.translate.setDefaultLang(this.lang);
    this.translate.use(this.lang);
  }

  // 语言切换的时候设置-当前项目只设置了中文和英文，所以是中英文切换
  changeLang(lang: string): void {
    if (lang === this.lang) {
      return;
    }
    localStorage.setItem('admin-lang', lang);
    this.translate.use(lang);
    this.lang = lang;
  }

}
